package com.example.ddd.domain.model;

/**
 * 订单项 - 值对象（不可变）
 */
public class OrderItem {
    private final String productId;
    private final int quantity;
    private final double price;
    
    public OrderItem(String productId, int quantity, double price) {
        if (productId == null || productId.isEmpty()) {
            throw new IllegalArgumentException("产品ID不能为空");
        }
        if (quantity <= 0) {
            throw new IllegalArgumentException("数量必须大于0");
        }
        if (price < 0) {
            throw new IllegalArgumentException("价格不能为负数");
        }
        
        this.productId = productId;
        this.quantity = quantity;
        this.price = price;
    }
    
    // 计算单项金额
    public double getAmount() {
        return quantity * price;
    }
    
    // Getters (没有setters，确保不可变性)
    public String getProductId() {
        return productId;
    }
    
    public int getQuantity() {
        return quantity;
    }
    
    public double getPrice() {
        return price;
    }
    
    // 重写equals和hashCode，根据值进行比较
    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        
        OrderItem orderItem = (OrderItem) o;
        
        if (quantity != orderItem.quantity) return false;
        if (Double.compare(orderItem.price, price) != 0) return false;
        return productId.equals(orderItem.productId);
    }
    
    @Override
    public int hashCode() {
        int result;
        long temp;
        result = productId.hashCode();
        result = 31 * result + quantity;
        temp = Double.doubleToLongBits(price);
        result = 31 * result + (int) (temp ^ (temp >>> 32));
        return result;
    }
}